package com.hook3.pianke.activity.infoLine;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hook3.pianke.Adapter.TalkAdapter;
import com.hook3.pianke.R;
import com.hook3.pianke.domain.TalkInfo;
import com.hook3.pianke.domain.TopicInfoDetail;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 用来显示webview和下面的评论
 * Created by HOME on 2015/12/12.
 */
public class WebInfo {

    private Activity activity;
    private View rootView;
    private TopicInfoDetail infoDetail;
    private FrameLayout web_info;
    private WebView talk_info_webView;
    private View talk_info_line_v_01;
    private LinearLayout talk_info_sort_view;
    private TextView talk_info_look_master_comment;
    private LinearLayout talk_info_translate_view;
    private ImageView talk_info_translate_img;
    private TextView talk_info_translate_tx;
    private TextView talk_info_hot_comment_tx;
    private ListView talk_info_line_v_02;
    private WebViewClient webViewClient = new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            rootView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    web_info.addView(inflate);
                }
            }, 300);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            if (url.contains("#PianKePic")) {
                //处理图片浏览器
                return true;
            }
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setData(Uri.parse(url));
            activity.startActivity(intent);
            return true;
        }
    };
    private View inflate;

    public WebInfo(Activity activity, View rootView, TopicInfoDetail infoDetail, FrameLayout web_info) {
        this.activity = activity;
        this.rootView = rootView;
        this.infoDetail = infoDetail;
        this.web_info = web_info;
        init();
    }

    private void init() {
        inflate = View.inflate(activity, R.layout.web_info_line, null);
        talk_info_webView = (WebView) inflate.findViewById(R.id.talk_info_webView);
        talk_info_line_v_01 = (View) inflate.findViewById(R.id.talk_info_line_v_01);
        talk_info_sort_view = (LinearLayout) inflate.findViewById(R.id.talk_info_sort_view);
        talk_info_look_master_comment = (TextView) inflate.findViewById(R.id.talk_info_look_master_comment);
        talk_info_translate_view = (LinearLayout) inflate.findViewById(R.id.talk_info_translate_view);
        talk_info_translate_img = (ImageView) inflate.findViewById(R.id.talk_info_translate_img);
        talk_info_translate_tx = (TextView) inflate.findViewById(R.id.talk_info_translate_tx);
        talk_info_hot_comment_tx = (TextView) inflate.findViewById(R.id.talk_info_hot_comment_tx);
        talk_info_line_v_02 = (ListView) inflate.findViewById(R.id.talk_info_line_v_02);
        initData();
    }

    private void initData() {
        // webview 解析
        TalkInfo postsinfo = infoDetail.getPostsinfo();
        talk_info_line_v_02.setAdapter(new TalkAdapter(activity, infoDetail));
        talk_info_webView.getSettings().setJavaScriptEnabled(true);
        talk_info_webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        talk_info_webView.setWebViewClient(webViewClient);
        String html = postsinfo.getHtml();
        try {
            html = getCss() + html + getJs();
        } catch (IOException e) {
            e.printStackTrace();
        }
        talk_info_webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
        talk_info_webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        talk_info_webView.setVerticalScrollBarEnabled(false);
        talk_info_webView.setHorizontalScrollBarEnabled(false);

        //talk_info_line_v_02.addHeaderView(talk_info_webView);
    }

    private String getJs()
            throws IOException {
        StringBuilder localStringBuilder = new StringBuilder();
        BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(activity.getAssets().open("article.js")));
        for (; ; ) {
            String str = localBufferedReader.readLine();
            if (str == null) {
                break;
            }
            localStringBuilder.append(str);
        }
        localBufferedReader.close();
        return localStringBuilder.toString();
    }

    private String getCss() throws IOException {
        StringBuilder localStringBuilder = new StringBuilder();
        BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(activity.getAssets().open("style.css")));
        for (; ; ) {
            String str = localBufferedReader.readLine();
            if (str == null) {
                break;
            }
            localStringBuilder.append(str);
        }
        localBufferedReader.close();
        return localStringBuilder.toString() + "";
    }


}
